package com.customview.CustomImagView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.customview.R;

/**
 * Created by Administrator on 2017/6/23.
 */

public class CustomImageView extends View {


    //属性设置
    String mTitlte;
    int mTitleColor;
    //图片
    Bitmap mBitmap;

    //实力画笔
    Paint mPaint;
    Rect mRect;

    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImagview, defStyleAttr, 0);

        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {

            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomImagview_myImage:
                    mBitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, R.mipmap.ic_launcher));
                    break;
                case R.styleable.CustomImagview_myTitle:
                    String string = typedArray.getString(attr);
                    break;
                case R.styleable.CustomImagview_myTitleColor:
                    mTitleColor = typedArray.getColor(attr, Color.GREEN);
                    break;
            }
            //回收
            typedArray.recycle();

            //初始化一些参数
            init();

        }

    }

    private void init() {
        mPaint = new Paint();

        //获取 字体的 高度 宽度
        mRect = new Rect();
        mPaint.getTextBounds(mTitlte, 0, mTitlte.length(), mRect);

    }


    //测量 view 的总体 高度 宽度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWith = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int Mwidth = 0;
        int Mheight=0;
        //根据模式来 决定宽度
        switch (modeWidth) {

            case MeasureSpec.EXACTLY://match_parent  精确的度数
                Mwidth = sizeWith;
                break;
            case MeasureSpec.AT_MOST://wrap_content  适配的度数

                //判断 文本长度 是否 大于   总体宽度
                int desireByTitle = getPaddingLeft() + getPaddingRight() + mRect.width();
                int desireByImage = getPaddingLeft() + getPaddingRight() + mBitmap.getWidth();

                int max = Math.max(desireByImage, desireByTitle);

                Mwidth = Math.min(modeWidth, max);

                break;
        }


        //根据模式来 决定宽度
        switch (modeHeight) {

            case MeasureSpec.EXACTLY://match_parent  精确的度数
                Mheight = sizeHeight;
                break;
            case MeasureSpec.AT_MOST://wrap_content  适配的度数

                Mheight = getPaddingBottom() + getPaddingTop() + mRect.height() + mBitmap.getHeight();


                break;
        }


        setMeasuredDimension(Mwidth, Mwidth);
    }

    //将view 绘制出来
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mRect.bottom=getHeight();
        mRect.left=0;
        mRect.top=0;
        mRect.right=getWidth();
        //设置  画笔的属性
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,16,getResources().getDisplayMetrics()));

    }
}
